<?php
/* 简易mongodb类
	code by 此题无解
	2014/6/12
 */


class Mon {
	private $mongodb;
	private $config;
	public  $db;
	public  $collection;
	public  $isConnect;
	public 	$errorInfo;

	function __construct($server,$options=array()){
		$this->isConnect = false;
		$this->config = $server;

		try {
			$this->mongodb = new Mongo($server,$options);
			$this->isConnect = true;
		} catch(MongoConnectionException $e) {
			$this->errorInfo = $e->getMessage();
		}
		
	}

	function __destruct(){
		if ($this->mongodb) {
			$this->mongodb->close();
		}
	}

	public function selectDB($db) {
		$this->db = $this->mongodb->selectDB($db);
		return $this->db;
	}

	public function selectCollection($collection) {
		$this->collection = $this->db->selectCollection($collection);
		return $this->collection;
	}

	public function insert($obj) {
		if (!$this->collection) return false;
		return $this->collection->insert($obj);
	}

	public function remove($obj=array()) {
		if (!$this->collection) return false;
		return $this->collection->remove($obj);
	}

	public function find() {
		if (!$this->collection) return false;
		return $this->collection->find();
	}

	public function update($o=array(),$n=array()){
		if (!$this->collection) return false;
		return $this->collection->update($o, $n);
	}

	public function listDBs(){
		if (!$this->mongodb) return false;
		return $this->mongodb->listDBs();
	}
}